LogTextEdit
Base classes
| Name |
Children |
Inherits |
PlainTextEdit prettyqt.widgets.plaintextedit Widget that is used to edit and display plain text. |
|
|
⋔ Inheritance diagram
graph TD
1509775594["custom_widgets.LogTextEdit"]
3482197220["widgets.PlainTextEdit"]
82444806["widgets.PlainTextEditMixin"]
1259044718["widgets.AbstractScrollAreaMixin"]
1689631995["widgets.FrameMixin"]
829357528["widgets.WidgetMixin"]
3227531497["core.ObjectMixin"]
2751680898["builtins.object"]
625385617["gui.PaintDeviceMixin"]
2639575484["QtWidgets.QPlainTextEdit"]
1459025998["QtWidgets.QAbstractScrollArea"]
3822057598["QtWidgets.QFrame"]
3620195106["QtWidgets.QWidget"]
3984060476["QtCore.QObject"]
37984305["Shiboken.Object"]
673956333["QtGui.QPaintDevice"]
3482197220 --> 1509775594
82444806 --> 3482197220
1259044718 --> 82444806
1689631995 --> 1259044718
829357528 --> 1689631995
3227531497 --> 829357528
2751680898 --> 3227531497
625385617 --> 829357528
2751680898 --> 625385617
2751680898 --> 829357528
2751680898 --> 1689631995
2751680898 --> 1259044718
2751680898 --> 82444806
2639575484 --> 3482197220
1459025998 --> 2639575484
3822057598 --> 1459025998
3620195106 --> 3822057598
3984060476 --> 3620195106
37984305 --> 3984060476
2751680898 --> 37984305
673956333 --> 3620195106
37984305 --> 673956333
🛈 DocStrings
Bases: PlainTextEdit
Source code in prettyqt/custom_widgets/logtextedit.py
| class LogTextEdit(widgets.PlainTextEdit):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rules = []
self.allow_wheel_zoom()
self.set_font(gui.Font.mono(as_qt=True))
self.append_text(f"Python version: {sys.version}")
new_logger = logging.getLogger()
# self.handler = signallogger.LineSignalLogger()
# self.handler.log_line.connect(self.append_text)
self.handler = signallogger.SignalLogger()
self.handler.signals.log_record.connect(self.append_record)
core.CoreApplication.call_on_exit(lambda: new_logger.removeHandler(self.handler))
self.handler.setLevel(logging.INFO)
new_logger.addHandler(self.handler)
fmt = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
self.set_formatter(fmt)
def set_formatter(self, formatter: logging.Formatter):
self.formatter = formatter
rules = [klass(self.formatter) for klass in Highlighter.__subclasses__()]
self.rules = [r for r in rules if r.is_included]
self.handler.setFormatter(formatter)
def append_record(self, record: logging.LogRecord):
start_of_line = len(self.text())
if self.formatter._fmt is None:
msg = "Formatter does not contain format string"
raise TypeError(msg)
self.append_text(self.formatter._fmt)
old_fmt = self.textCursor().charFormat()
with self.selecter.create_cursor() as c:
c.move_position("end")
c.move_position("start_of_block")
start_pos = c.position()
for r in self.rules:
line_text = c.select_text(start_pos, "end_of_block")
matches = list(r.pattern.finditer(line_text))
for m in reversed(matches):
pos = m.start(0) + start_of_line
if start_of_line != 0:
pos += 1
end = pos + m.end(0) - m.start(0)
fmt_string = r.format_string(record)
try:
value = f"%{m.group(1)}" % fmt_string
except TypeError, ValueError:
value = fmt_string
c.replace_text(pos, end, value)
fmt = r.get_format(fmt_string)
c.setCharFormat(fmt)
c.clearSelection()
c.setCharFormat(old_fmt)
|
:fa-brands-github: Show source on GitHub